package com.samoiloff.util.lang {
	import com.samoiloff.util.data.ArrItem;

	/**
	 * @author NM Group
	 */
	public class XMLIdsLangItem extends ArrItem {
		public function XMLIdsLangItem() {
			
		}
		
		public function update(xml:XML):void {
			for each (var itemXML:XML in xml.item) {
				if (String(itemXML.@ids).length > 0) {
					var newLangItem:XMLIdLangItem = new XMLIdLangItem(itemXML);
					var oldLangItem:XMLIdLangItem = getById(newLangItem.ids[0]);
					if (oldLangItem) {
						removeItem(oldLangItem);
					}
					_arr.push(newLangItem);
				}
			}
		}

		public function getItemAt(i:int): XMLIdLangItem {
			return XMLIdLangItem(_arr[i]);
		}
		
		public function containsId(id:*):Boolean {
			for (var i:int = 0; i<length; i++) {
				var langItem:XMLIdLangItem = getItemAt(i);
				var value:Object = langItem.containsId(String(id)) ? langItem.value : null;
				if (value) {
					return true;
				}
			}
			return false;
		}
		
		public function getItemById(id:*): * {
			for (var i:int = 0; i<length; i++) {
				var langItem:XMLIdLangItem = getItemAt(i);
				var value:Object = langItem.containsId(String(id)) ? langItem.value : null;
				if (value) {
					return value;
				}
			}
			return "Name not found for id = " + id;
		}

		public function getById(id:*): XMLIdLangItem {
			for (var i:int = 0; i<length; i++) {
				var langItem:XMLIdLangItem = getItemAt(i);
				if (langItem.containsId(String(id))) {
					return langItem;
				}
			}
			return null;
		}
	}
}
